import React, { Component } from 'react'
import Layout from 'components/Layout'
import { Switch, Route, Redirect } from 'react-router-dom'
import adminRoutes from 'routes'
export default class Admin extends Component {
  render() {
    return (
      <div>
        <Layout>
          <Switch>
            {
              adminRoutes.map(route=>{
                return (
                  <Route 
                    key={route.path}
                    path={route.path}
                    render={
                      (routeProps)=>{
                        // 拿到当前路由的roles  和 当前 用户的role 进行比较
                        const { role } = JSON.parse(localStorage.getItem('userInfo'))
                        const roles = route.meta.roles;
                        const hasPerssion = roles.some(el=>el===role)
                        if(hasPerssion){
                          // 有权限
                          return <route.component {...routeProps}/>
                        }else{
                          // 没有权限
                          return <Redirect to="/admin/noPerssion"/>
                        }
                      }
                    }
                  />
                )
              })
            }
            <Redirect to="/admin/dashBoard" from="/admin" exact/>
            <Redirect to="/404"/>
          </Switch>
        </Layout>
      </div>
    )
  }
}
